<?php require 'inc_security.php'; 
//Kiem tra quyen addedit
checkPermission('edit');

$record_id = getValue('record_id');

$adm_birthday = convertDateTime(getValue('adm_birthday','str','POST',''));
$myform = new generate_form();
$myform->addTable($bg_table);
$myform->add('adm_birthday','adm_birthday',1,1,0);
$myform->add('adm_mail','adm_mail',0,0,'');
$myform->add('adm_name','adm_name',0,0,'');
$myform->add('adm_phone','adm_phone',0,0,'');
$myform->add('adm_active','adm_active',1,0,0);
$action = getValue('action','str','POST','');
if($action == 'execute'){
	$bg_errorMsg .= $myform->checkData();
	if(!$bg_errorMsg){
		$db_ex = new db_execute($myform->generate_update_SQL($id_field,$record_id));
		unset($db_ex);
		//Delete du lieu cu
		$db_delete = new db_execute('DELETE FROM admin_users_right
									WHERE adu_admin_id = '.$record_id);
		unset($db_delete);
		//insert admin_users_right
		$record_module = getValue('module_id','arr','POST','');
		if($record_module){
			$count = count($record_module);
			for($i = 0; $i<$count; $i++){
				$module_id = $record_module[$i];
				$myform = new generate_form();
				$myform->add('adu_admin_id','record_id',1,1,0);
				$myform->add('adu_admin_module_id','module_id',1,1,0);
				$myform->add('adu_admin_edit','adu_edit'.$record_module[$i], 1, 0, 0);
				$myform->add('adu_admin_add','adu_add'.$record_module[$i], 1, 0, 0);
				$myform->add('adu_admin_delete','adu_delete'.$record_module[$i], 1, 0, 0);
				$myform->addTable('admin_users_right');
				$db_insert = new db_execute($myform->generate_insert_SQL());
			}
		}
		redirect('listing.html');
	}
}
//lấy dữ liệu record cần sửa đổi
$db_data 	= new db_query("SELECT * FROM " . $bg_table . " WHERE " . $id_field . " = " . $record_id);
if($row 		= mysql_fetch_assoc($db_data->result)){
	foreach($row as $key=>$value){
		$$key = $value;
	}
}else{
		exit();
} 
$db_getallmodule = new db_query('SELECT * 
								 FROM modules
								 ORDER BY mod_order DESC');
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="vi" lang="vi" xmlns:og="http://ogp.me/ns#" xmlns:fb="https://www.facebook.com/2008/fbml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?=$load_header?>
<style>
	.list-role label {
		margin-bottom: 0;
		margin-top : 6px;
		margin-right : 20px;
	}
	.list-role input[type="checkbox"]{
		margin-top:-1px;
		margin-right:2px;
	}
</style>
</head>
<body>
<div class="module_header bold fix"><?=$module_name?></div>
<div id="wrapper">
    <?=$bg_errorMsg?>
	<?php $form = new form();
	?>
	<?=$form->form_open()?>
    <?=$form->textnote('Các trường có dấu (<span class="form-asterick">*</span>) là bắt buộc nhập')?>
	<div class="control-group">
		<label class="control-label">Tên đăng nhập :</label>
		<div class="controls"><span class="uneditable-input"><?=$adm_loginname?></span></div>
	</div>
	<?=$form->text(array('label'=>'Email','name'=>'adm_mail','id'=>'adm_mail','value'=>$adm_mail))?>
	<?=$form->text(array('label'=>'Họ tên','name'=>'adm_name','id'=>'adm_name','value'=>$adm_name))?>
	<?=$form->text(array('label'=>'Điện thoại','name'=>'adm_phone','id'=>'adm_phone','value'=>$adm_phone))?>
	<?=$form->text(array('label'=>'Ngày sinh','name'=>'adm_birthday','id'=>'adm_birthday','isdatepicker'=>1,'value'=>date('d/m/Y',$adm_birthday)))?>
	<?=$form->checkbox(array('label'=>'Kích hoạt','name'=>'adm_active','id'=>'adm_active','value'=>1,'currentValue'=>$adm_active))?>
	<?//Phan quyen quan tri?>
	<h3 style="padding-left:170px;font-size : 13px;background:#f0f0f0">Phân quyền module</h3>
	<? 
	while ($row=mysql_fetch_array($db_getallmodule->result)){
        if(file_exists("../../modules/" . $row["mod_path"] . "/inc_security.php")===false){
            if(file_exists("../../core/" . $row["mod_path"] . "/inc_security.php")===false){
                continue;
            }
        }
        //Check add edit module nay
        $db = new db_query('SELECT adu_admin_add AS `add`, adu_admin_edit AS `edit`, adu_admin_delete AS `del`
                            FROM admin_users_right
                            WHERE adu_admin_id = '.$record_id.' AND adu_admin_module_id = '.$row['mod_id']);
        $result_check = mysql_fetch_assoc($db->result);unset($db);
        $rs_md = $result_check ? 'checked="checked"' : '';
        $rscheck_add = $result_check['add'] ? 'checked="checked"' : '';
        $rscheck_edit = $result_check['edit'] ? 'checked="checked"' : '';
        $rscheck_delete = $result_check['del'] ? 'checked="checked"' : '';
	?>
	<div class="control-group">
		<label class="control-label"><?=$row['mod_name']?></label>
		<div class="controls list-role">
			<label class="fl"><input type="checkbox" name="module_id[]" value="<?=$row['mod_id']?>" <?=$rs_md?> >Truy cập</label>
			<label class="fl"><input type="checkbox" name="adu_add<?=$row['mod_id']?>" value="1" <?=$rscheck_add?> >Thêm</label>
			<label class="fl"><input type="checkbox" name="adu_edit<?=$row['mod_id']?>" value="1" <?=$rscheck_edit?> >Sửa</label>
			<label class="fl"><input type="checkbox" name="adu_delete<?=$row['mod_id']?>" value="1" <?=$rscheck_delete?> >Xóa</label>
		</div>
	</div>
    <?}?>
    <?=$form->form_action(array('label'=>array('Cập nhật','Nhập lại'),'type'=>array('submit','reset')))?>
	<?=$form->form_close()?>
</div>
</body>
</html>